﻿using System;
using System.Collections.Generic;
using System.IO;
using Catel.Articles._02___WPF_controls.Data;
using Catel.Articles.Base.Data.Attributes;
using Catel.Articles.Base.UI;
using System.Windows.Markup;

namespace Catel.Articles._02___WPF_controls.Examples.DropDownButton
{
    [Example("DropDownButton", "Example of the DropDownButton control",
        LongDescription = @"The DropDownButton is a very well known control, so it's weird that the WPF developers didn't include this control.

The DropDownButton has a custom Content property so it is fully customizable.

This example shows how to use the control, but also how to add nice styles to a ListBox control as well.",
        SourceFileName = "/Examples/DropDownButton/Code.txt",
        ImageUri = "/Examples/DropDownButton/Screenshot.png",
        ClickHandlerName = "ShowExample")]
    public class Example
    {
        /// <summary>
        /// Shows the example.
        /// </summary>
        private static void ShowExample()
        {
            // Declare variables
            Windows.Controls.DropDownButton dropDownButton = null;

            // Create context
            ParserContext context = new ParserContext();

            // Load xaml namespaces differently
            context.XamlTypeMapper = new XamlTypeMapper(new string[] { });
            context.XamlTypeMapper.AddMappingProcessingInstruction("Controls", "Catel.Windows.Controls", "Catel.Windows");
            context.XamlTypeMapper.AddMappingProcessingInstruction("LocalControls", "Catel.Articles._02___WPF_controls.UI.Controls", "Catel.Articles.02 - WPF controls");

            // Now load the xmlns dictionary
            context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            context.XmlnsDictionary.Add("Controls", "Controls");
            context.XmlnsDictionary.Add("LocalControls", "LocalControls");

            // Load control via xaml
            using (Stream stream = new FileStream("Examples\\DropDownButton\\Code.txt", FileMode.Open))
            {
                dropDownButton = (Windows.Controls.DropDownButton)XamlReader.Load(stream, context);
            }

            // Create dummy data context
            dropDownButton.DataContext = new { PersonCollection = CreatePersonCollection() };

            // Show control
            ExampleHelper.ShowControlInWindow(dropDownButton);
        }

        /// <summary>
        /// Creates a person collection.
        /// </summary>
        /// <returns>List of persons.</returns>
        private static List<Person> CreatePersonCollection()
        {
            // Declare variables
            List<Person> personCollection = new List<Person>();

            personCollection.Add(new Person()
            {
                FirstName = "Geert",
                MiddleName = "van",
                LastName = "Horrik",
                Birthday = new DateTime(1984, 8, 1),
                PassportPhoto = "/Resources/Images/nerd.png"
            });

            personCollection.Add(new Person()
            {
                FirstName = "John",
                MiddleName = "",
                LastName = "Doe",
                Birthday = new DateTime(2000, 1, 1),
                PassportPhoto = "/Resources/Images/anonymous.png"
            });

            // Return result);
            return personCollection;
        }
    }
}
